<script setup>
import addColor from "../../components/Color/AddDialog.vue"
import updateColor from "../../components/Color/UpdateDialog.vue"
import {onMounted, reactive, ref} from 'vue'
import {deleteColor, findColors} from "@/apis/zyj/color.js"
import {
  ElButton,
  ElForm,
  ElFormItem,
  ElInput,
  ElMessage,
  ElMessageBox,
  ElPagination,
  ElTable,
  ElTableColumn
} from 'element-plus'

const tableData = reactive([])
const select_order_number = ref('') //表格select选中的条数
const select_orderId = ref([]) //表格select复选框选中的id
const multipleSelection = ref()//选中行的数据
const multipleTable = ref()//用于表格多选框选中的清理
//显示添加对话框
const addDialog = ref(false)
//修改对话框
const updateDialog = ref(false)

//查询列表
function findColorList() {
  findColors(searchForm, currentPage.value, pageSize.value).then(resp => {
    console.log(resp)
    tableData.length = 0
    tableData.push(...resp.records);
    total.value = resp.total;
    currentPage.value = resp.current;
  })
}

//初始化
onMounted(() => {
  findColorList();
})
//搜索表单
const searchForm = reactive({
  colorCode: '',
  color: '',
  remarks: ''
})
//分页
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
//搜索按钮
const handleSearch = () => {
  findColorList()
}
//重置按钮
const handleReset = () => {
  Object.assign(searchForm, {
    colorCode: '',
    color: '',
    remarks: ''
  })
}

//新增事件
const handleAdd = () => {
  addDialog.value = true
}
//关闭添加对话框
const closeDialog = () => {
  addDialog.value = false;
  updateDialog.value = false;
  findColorList()
  multipleTable.value.clearSelection();
};
//修改事件
const handleModify = (row) => {
  if (row.id != null) {
    multipleSelection.value = {...row}
  } else {
    multipleSelection.value = {...multipleSelection.value[0]}
  }
  updateDialog.value = true
}
//删除事件
const handleDelete = (row) => {
  if (row.id != null) {
    select_order_number.value = "1";
  } else {
    select_order_number.value = multipleSelection.value.length;
  }
  ElMessageBox.confirm(
      '是否要删除 ' + select_order_number.value + '条数据',
      '警告',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(() => {
        if (row.id != null) {
          select_orderId.value = [];
          select_orderId.value.push(row.id)
        }
        deleteColor(select_orderId.value).then((res) => {
          if (res == '成功') {
            findColorList()
            ElMessage.success('成功删除: ' + select_order_number.value + '条数据')
            select_order_number.value = 0;
            select_orderId.value = [];
            multipleTable.value.clearSelection();
          } else {
            ElMessage.error('系统错误！删除失败')
          }
        })
      })
      .catch(() => {
        ElMessage.info('删除取消')
      })
}
//分页事件
const handleSizeChange = () => {
  findColorList();
}
//当前页码事件
const handleCurrentChange = () => {
  findColorList();
}
//
const getRowKeys = (row) => {
  //记录每行的key值
  return row.id;
}
//选中事件
const handleSelectionChange = (val) => {
  // 解决来回切换页面，也无法清除上次选中情况
  multipleSelection.value = val;
  select_order_number.value = multipleSelection.value.length;
  select_orderId.value = [];
  if (val) {
    val.forEach((row) => {
      if (row) {
        select_orderId.value.push(row.id);
      }
    });
  }
}
</script>

<template>

  <div class="color-management">
    <el-card>
      <ElForm :inline="true" :model="searchForm" class="demo-form-inline">
        <ElFormItem label="色号">
          <ElInput v-model="searchForm.colorCode" placeholder="请输入色号"/>
        </ElFormItem>
        <ElFormItem label="颜色">
          <ElInput v-model="searchForm.color" placeholder="请输入颜色"/>
        </ElFormItem>
        <ElFormItem label="备注">
          <ElInput v-model="searchForm.remarks" placeholder="请输入备注"/>
        </ElFormItem>
        <ElFormItem>
          <ElButton type="primary" @click="handleSearch">搜索</ElButton>
          <ElButton @click="handleReset">重置</ElButton>
        </ElFormItem>
      </ElForm>
    </el-card>
    <el-card style="margin-top: 20px">
      <div class="action-buttons">
        <ElButton type="primary" @click="handleAdd" v-prem="'produce:prodBasicData:color:insert'" plain>新增</ElButton>
        <ElButton type="success" :disabled="select_order_number!=1" @click="handleModify"
                  v-prem="'produce:prodBasicData:color:update'" plain>修改
        </ElButton>
        <ElButton type="danger" :disabled="select_order_number==0" @click="handleDelete"
                  v-prem="'produce:prodBasicData:color:delete'" plain>删除
        </ElButton>
      </div>

      <ElTable :data="tableData" ref="multipleTable" :row-key="getRowKeys" @selection-change="handleSelectionChange"
               :row-style="{height:'60px'}" style="width: 100%;" max-height="380px">
        <ElTableColumn type="selection" :reserve-selection="true" width="55"/>
        <ElTableColumn prop="colorCode" label="色号" width="180"/>
        <ElTableColumn prop="color" label="颜色" width="180"/>
        <ElTableColumn prop="remark" label="备注"/>
        <ElTableColumn prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.status === '正常' ? 'success' : 'danger'">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </ElTableColumn>
        <ElTableColumn label="操作" width="150">
          <template #default="scope">
            <ElButton link size="small" type="primary" @click="handleModify(scope.row)"
                      v-prem="'produce:prodBasicData:color:update'">修改
            </ElButton>
            <ElButton link size="small" type="danger" @click="handleDelete(scope.row)"
                      v-prem="'produce:prodBasicData:color:delete'">删除
            </ElButton>
          </template>
        </ElTableColumn>
      </ElTable>

      <ElPagination
          v-model:currentPage="currentPage"
          v-model:pageSize="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :small="false"
          :disabled="false"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </el-card>
    <add-color :addDialog="addDialog" @close-dialog="closeDialog"/>
    <update-color :updateDialog="updateDialog" :form="multipleSelection" @close-dialog="closeDialog"/>
  </div>
</template>

<style scoped>
.color-management {
  padding: 20px;
}

.action-buttons {
  margin-bottom: 20px;
}

.el-pagination {
  margin-top: 20px;
  justify-content: flex-end;
}
</style>